<?php
namespace app\stores\model\promotion;

use app\beautiful\model\goods\GoodsGroupModel;
use app\common\model\CommonModel;
use think\facade\Db;

class ActivityModel extends CommonModel{
    protected  $name = 'promotion_activity';
    protected  $pk   = 'activity_id';

    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function info($where,$field='*'){
        $info = Db::name('promotion_activity')->alias('a')
            ->join('wyu_beautiful_goods g','a.goods_id=g.goods_id')
            ->join('wyu_shop_detail sd','sd.shop_id=g.shop_id')
            ->join('wyu_shop s','s.shop_id=sd.shop_id')
            ->where(['a.is_delete'=>0])
            ->where($where)
            ->field($field)
            ->find();
        if(!empty($info)){
            $info['team_ids'] = $info['team_ids']?array_map('intval',explode(',',$info['team_ids'])):[];
            $info['team'] = TeamModel::whereIn("id",$info['team_ids'])->field('id,name')->select();
            $info['start_time'] = date('Y-m-d',strtotime($info['start_time']));
            $info['end_time'] = date('Y-m-d',strtotime($info['end_time']));
            $info['sell_start_time'] = $info['sell_time_type']>0?date('Y-m-d',strtotime($info['sell_start_time'])):$info['sell_start_time'];
            $info['sell_end_time'] = $info['sell_time_type']>0?date('Y-m-d',strtotime($info['sell_end_time'])):$info['sell_end_time'];
            $info['use_limit_detail'] = $info['use_limit_detail']?json_decode($info['use_limit_detail'],true):$info['use_limit_detail'];
            if(isset($info['type']) && $info['type']==3){
                //套餐分组
                $goods_group = GoodsGroupModel::with(['groupDetail'=>function($query){
                        $query->where(['is_delete'=>0]);
                    }])->where([['goods_id','=',$info['goods_id']],['is_delete','=',0]])
                        ->field('goods_group_id,group_name,type,num,choose_num,is_choose_same')
                        ->select()->toArray() ?? [];
                $info['goods_group'] = $goods_group;
                $info['original_price'] = $info['total_value'];
            }

        }
        return $info;
    }

    public static function getId($where){
        $activity_ids = Db::name('promotion_activity')->alias('a')
            ->join('wyu_beautiful_goods g','a.goods_id=g.goods_id')
            ->where(['a.is_delete'=>0,'g.is_delete'=>0])
            ->where($where)
            ->column('a.activity_id');
        return $activity_ids;
    }
    public static function list($where,$order,$field='*',$page=1,$limit=10){
        $query = Db::name('promotion_activity')->alias('a')
            ->join('wyu_beautiful_goods g','a.goods_id=g.goods_id')
            ->join('wyu_shop_detail sd','sd.shop_id=g.shop_id')
            ->join('wyu_shop s','s.shop_id=sd.shop_id')
            ->where(['a.is_delete'=>0,'g.is_delete'=>0])
            ->when(isset($where['shop_id']) && !empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['type']),function ($query) use ($where){
                $query->where(['a.type'=>$where['type']]);
            })
            ->when(isset($where['not_activity_ids']) && !empty($where['not_activity_ids']),function ($query) use ($where){
                $query->whereNotIn('a.activity_id',$where['not_activity_ids']);
            })
            ->when(isset($where['name']) && !empty($where['name']),function ($query) use ($where){
                $query->where('g.name','like','%'.$where['name'].'%');
            })
            ->when(isset($where['status']) && !empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(isset($where['activity_ids']) && !empty($where['activity_ids']),function ($query) use ($where){
                $query->whereIn('a.activity_id',$where['activity_ids']);
            })
            ->when(isset($where['start_sales_num']) && isset($where['end_sales_num']) && $where['start_sales_num']>=0 && $where['end_sales_num']>=0,function ($query) use ($where){
                $query->where('a.sales_num>='.$where['start_sales_num'].' and a.sales_num<='.$where['end_sales_num']);
            })
            ->field($field);
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                if($v['type']==3) $v['original_price'] = $v['total_value'];
                unset($v['total_value']);
                $v['start_time'] = date('Y-m-d',strtotime($v['start_time']));
                $v['end_time'] = date('Y-m-d',strtotime($v['end_time']));
                $v['sell_start_time'] = $v['sell_time_type']>0?date('Y-m-d',strtotime($v['sell_start_time'])):$v['sell_start_time'];
                $v['sell_end_time'] = $v['sell_time_type']>0?date('Y-m-d',strtotime($v['sell_end_time'])):$v['sell_end_time'];
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
}
